# class Student(object):
#     def __init__(self, name, score):
#         self.name = name
#         self.score = score

#     def print_score(self):
#         print(f'{self.name}: {self.score}')

#     def get_grade(self):
#         if self.score >= 90:
#             return 'A'
#         elif self.score >= 60:
#             return 'B'
#         else:
#             return 'C'

# bart = Student('Bart Simpson',60)
# lisa = Student('Lisa',99)

# #面向对象编程 对象是主体
# bart. print_score()

# print(lisa.name,lisa.get_grade()) #调用函数 函数名后面要加上括号



#限制访问

# class Student(object):
#     def __init__(self, name, score):
#         self.__name = name
#         self.__score = score

#     def print_score(self):
#         print(f'{self.__name}: {self.__score}')

# #如果外部代码要获取name和score怎么办？可以给Student类增加get_name和get_score这样的方法
#     def get_name(self):
#         return self.__name
    
# #如果又要允许外部代码修改score怎么办？可以再给Student类增加set_score方法：
#     def set_score(self,score):
#         self.__score = score

# #直接通过bart.score = 99也可以修改啊，为什么要定义一个方法大费周折？因为在方法中，可以对参数做检查，避免传入无效的参数：
#     def set_score_check(self,score):
#         if  0 <= score <= 100:
#             self.__score = score
#         else:
#             raise ValueError('wrong score!')



# #练习 请把下面的Student对象的gender字段对外隐藏起来，用get_gender()和set_gender()代替，并检查参数有效性：
# class Student(object):
#     def __init__(self, name, gender):
#         self.name = name
#         self.__gender = gender
    
#     def get_gender(self):
#         return self.__gender
    
#     def set_gender(self,gender):
#         if gender == 'male':
#             self.__gender = gender
#         elif gender == 'female':
#             self.__gender = gender
#         #还可以合并成 if gender in ['male','female']
#         else:
#             raise ValueError('wrong gender!')



# # 测试:
# bart = Student('Bart', 'male')
# if bart.get_gender() != 'male':
#     print('测试失败!')
# else:
#     bart.set_gender('female')
#     if bart.get_gender() != 'female':
#         print('测试失败!')
#     else:
#         print('测试成功!')




#继承与多态
class Animal(object):
    def run(self):
        print('Animal is running...')

class Dog(Animal):
    pass

class Cat(Animal):
    def run(self):
        print('Cat is running...')

dog = Dog()
dog.run()

cat = Cat()
cat.run()